package com.qdxwx.manage.controller.member;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.member.MemberSaleService;
import com.qdxwx.data.service.member.MemberTypeService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.enums.BusinessType;
import com.qdxwx.models.member.MemberSale;
import com.qdxwx.models.member.MemberType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 会员商品Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/member/sale")
public class MemberSaleController extends BaseController {
    @Autowired
    private MemberSaleService memberSaleService;
    @Autowired
    private MemberTypeService memberTypeService;

    @GetMapping("list.html")
    public String list(ModelMap mmap) {
        MemberType memberType = new MemberType();
        memberType.setStatus(0);
        List<MemberType> list = memberTypeService.getList(memberType, null);
        mmap.put("memberType", list);
        return "member/member-sale-list";
    }

    /**
     * 查询会员商品列表
     */
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        MemberSale memberSale = new MemberSale();
        memberSale.setStatus(0);
        memberSale.setName(TKString.queryKey(request.getParameter("name")));
        memberSale.setTypeId(TKConvert.toInteger(request.getParameter("type")));
        List<MemberSale> list = memberSaleService.getList(memberSale, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(memberSaleService.count(memberSale));
        return rspData;
    }

    /**
     * 新增会员商品
     */
    @GetMapping("/add.html")
    public String add(ModelMap mmap) {
        MemberType memberType = new MemberType();
        memberType.setStatus(0);
        List<MemberType> list = memberTypeService.getList(memberType, null);
        mmap.put("memberType", list);
        return "member/member-sale-add";
    }

    /**
     * 修改会员商品
     */
    @GetMapping("/edit.html")
    public String edit(HttpServletRequest request, ModelMap mmap) {
        int id = TKConvert.toInteger(request.getParameter("id"));
        if (id < 1) return error404;
        MemberType memberType = new MemberType();
        memberType.setStatus(0);
        List<MemberType> list = memberTypeService.getList(memberType, null);
        mmap.put("memberType", list);
        MemberSale memberSale = memberSaleService.get(id);
        mmap.put("memberSale", memberSale);
        return "member/member-sale-edit";
    }

    /**
     * 新增保存会员商品
     */
    @Log(title = "会员商品", businessType = BusinessType.INSERT)
    @PostMapping("/save.json")
    @ResponseBody
    public AjaxResult save(HttpServletRequest request) {
        String name = request.getParameter("name");
        if (TKString.isEmpty(name)) return error("请输入商品名称");
        int typeId = TKConvert.toInteger(request.getParameter("typeId"));
        if (typeId < 1) return error("请选择会员类型");
        int price = Double.valueOf(TKConvert.toDouble(request.getParameter("price")) * 100).intValue();
        if (price <= 0) return error("商品价格必须大于0");
        int days = TKConvert.toInteger(request.getParameter("days"));
        if (days < 1) return error("商品开通时长必须大于1");
        long id = TKConvert.toLong(request.getParameter("id"));
        MemberSale memberSale = null;
        if (id > 0) {
            memberSale = memberSaleService.get(id);
            if (memberSale == null) return error("找不到该会员商品");
        } else {
            memberSale = new MemberSale();
            memberSale.setStatus(0);
        }
        memberSale.setName(name);
        memberSale.setTypeId(typeId);
        memberSale.setPrice(price);
        memberSale.setDays(days);
        memberSale.setSort(TKConvert.toInteger(request.getParameter("sort")));
        memberSale.setDescription(request.getParameter("description"));
        int row;
        if (id > 0) {
            row = memberSaleService.update(memberSale);
        } else {
            row = memberSaleService.insert(memberSale);
        }
        return toAjax(row);
    }

    /**
     * 删除会员商品
     */
    @Log(title = "会员商品", businessType = BusinessType.DELETE)
    @PostMapping("/remove.json")
    @ResponseBody
    public AjaxResult remove(HttpServletRequest request) {
        String ids = request.getParameter("ids");
        if (TKString.isEmpty(ids)) return error("参数异常");
        return toAjax(memberSaleService.delete(ids));
    }
}
